//引入express模块
const express = require('express');
//引入数据库连接池模块
const pool = require('../pool.js');
//创建路由器对象
const r = express.Router();


//1.查询攻略首页随机标签 http://127.0.0.1:3000/strategy/randomlabel
r.get("/randomlabel",(req,res)=>{
	let sql="select * from strategy_yzj order by rand()";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//2.查询攻略首页分类标题 http://127.0.0.1:3000/strategy/category
r.get("/category",(req,res)=>{
	let sql="select distinct category from strategy_yzj";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//3.查询攻略首页导航图文 http://127.0.0.1:3000/strategy/titleimg
r.get("/titleimg",(req,res)=>{ 
	let sql=`select * from strategy_yzj`;
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//4.查询攻略首页精选文章列表 http://127.0.0.1:3000/strategy/bestlist
r.get("/bestlist",(req,res)=>{
	let sql="select * from strategy_yzj where isChosen='1'";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//6.查询攻略首页热门top榜  http://127.0.0.1:3000/strategy/hotlist
r.get("/hotlist",(req,res)=>{
	let sql="select * from strategy_yzj order by scan desc";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});


//1.查询攻略列表页分类下的所有子类  http://127.0.0.1:3000/strategy/subcat/:category
r.get("/subcat",(req,res)=>{
	// 通过传参得到要查询的类别
	let category=req.query.category; 
	let sql="select distinct subcat from strategy_yzj where category=?";
	pool.query(sql,[category],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

// 2.查询攻略列表页子类所在分类的所有子类  
// http://127.0.0.1:3000/strategy/categories/:subcat
r.get("/categories",(req,res)=>{
	// 通过传参得到要查询的类别
	let subcat=req.query.subcat; 
	let sql="select * from strategy_yzj where subcat=?";
	pool.query(sql,[subcat],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//3.查询攻略列表页分类文章,不分页,只截取  http://127.0.0.1:3000/strategy/clist/:category
r.get("/clist",(req,res)=>{

	// 通过传参得到要查询的类别
	let category=req.query.category; 
	let sql="select * from strategy_yzj where category=?";
	pool.query(sql,[category],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//4.查询攻略列表页子类文章,不分页,只截取  http://127.0.0.1:3000/strategy/slist/:subcat
r.get("/slist",(req,res)=>{

	// 通过传参得到要查询的类别
	let subcat=req.query.subcat; 
	let category=req.query.category;
	let sql="select * from strategy_yzj where category=? and subcat=?";
	pool.query(sql,[category,subcat],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

//5.查询攻略列表页最新攻略  http://127.0.0.1:3000/strategy/newlist
r.get("/newlist",(req,res)=>{
	let sql="select * from strategy_yzj order by sendtime desc";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

// 6.查询搜索相关攻略列表  http://127.0.0.1:3000/strategy/search
r.get("/search",(req,res)=>{
	let search=req.query.search;
	let sql="select * from strategy_yzj where title like ?";
	pool.query(sql,["%"+search+"%"],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});

// 1.查询攻略内容页正文  http://127.0.0.1:3000/strategy/scontent:yid
r.get("/scontent",(req,res)=>{
	let yid=req.query.yid;
	let sql="select * from strategy_yzj where yid=?";
	pool.query(sql,[yid],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});


// 2.查询评论页正文 http://127.0.0.1:3000/strategy/scomment:yid
r.get("/scomment",(req,res)=>{
	let yid=req.query.yid;
	let sql="select * from scomment_yzj where strategy_id=? order by ctime asc";
	pool.query(sql,[yid],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});
// 查询登录用户部分数据 http://127.0.0.1:3000/strategy/suser
// 3.增加评论页正文 http://127.0.0.1:3000/strategy/scomtxt:scom
r.get("/suser",(req,res)=>{
	let phone=req.query.phone;
	let sql="select uid,uname from wedding_user where phone=?";
	pool.query(sql,[phone],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});
r.post("/scomtxt",(req,res)=>{
	let scom=req.body;
	// console.log(scom)
	// scom.ctime=scom.ctime.slice(0,10);
	// console.log(scom);
	let sql="insert into scomment_yzj set ?";
	pool.query(sql,[scom],(err,result)=>{
		if(err)throw err;
		// console.log(result);
		res.send(result);
	});
});















//导出路由对象
module.exports=r;